package com.dwxt.cdms.service;

import com.dwxt.cdms.api.common.utils.DateUtil;
import com.dwxt.cdms.dao.BlutdruckMapper;
import com.dwxt.cdms.entity.HeartVoice;
import com.dwxt.cdms.entity.oufu.OuFuData;
import com.dwxt.cdms.entity.vo.BlutdruckCustom;
import com.dwxt.common.base.BaseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TVService {

    @Autowired
    private BlutdruckMapper blutdruckMapper;

    @Autowired
    private OuFuService ouFuService;

    @Autowired
    private HeartVoiceService heartVoiceService;



    /**
    * @Description: 通过用户id查询血压仪或者oufu手表血压
    * @Param [customerId, scope]
    * @Return com.dwxt.common.base.BaseResult
    * @Author chendq
    * @Date 2021/12/2
    **/
    public BaseResult selectBlutdruckListForTV(String customerId, Integer scope) {
        //血压仪
        List<BlutdruckCustom> blutdruckCustoms = blutdruckMapper.selectBloodPressureList(customerId,scope,0);
        if (blutdruckCustoms.size() > 0) {
            return BaseResult.ok(blutdruckCustoms);
        }
        //oufu
        List<OuFuData> ouFuDataList = ouFuService.selectCustomerOufu(customerId, scope, "8");
        if (ouFuDataList.size() > 0) {
            BlutdruckCustom blutdruckCustom = new BlutdruckCustom();
            List<BlutdruckCustom> collect = ouFuDataList.stream().map(ouFuData -> {
                blutdruckCustom.setCustomerId(Integer.valueOf(customerId));
                blutdruckCustom.setHighData(ouFuData.getDiastolic().toString());
                blutdruckCustom.setLowData(ouFuData.getShrink().toString());
                blutdruckCustom.setCheckTime(DateUtil.parseStr(ouFuData.getCheckTime(), DateUtil.FMT_YYYYMMDD_HHMMSS));
                ZoneId zoneId = ZoneId.systemDefault();
                Instant instant = ouFuData.getCreateTime().toInstant();
                LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
                blutdruckCustom.setCreateTime(localDateTime);
                return blutdruckCustom;
            }).collect(Collectors.toList());
            return BaseResult.ok(collect);
        }
        return BaseResult.ok();
    }


    /**
     * @Description: 通过用户id查询心电仪或者oufu心率
     * @Param [customerId, scope]
     * @Return com.dwxt.common.base.BaseResult
     * @Author chendq
     * @Date 2021/12/2
     **/
    public BaseResult selectHeartVoiceListForTV(String customerId, Integer scope) {
        //心电仪
        List<HeartVoice> heartVoices = heartVoiceService.selectCustomerHeartVoice(customerId, scope);
        if (heartVoices.size() > 0) {
            return BaseResult.ok(heartVoices);
        }
        //oufu
        List<OuFuData> ouFuDataList = ouFuService.selectCustomerOufu(customerId, scope, "6");
        if (ouFuDataList.size() > 0) {
            List list = new ArrayList();
            for (OuFuData ouFuData : ouFuDataList) {
                HeartVoice heartVoice = new HeartVoice();
                heartVoice.setAvgHr(ouFuData.getHeartbeat());
                heartVoice.setCheck_time(ouFuData.getCheckTime());
                heartVoice.setCustomerId(ouFuData.getCustomerId());
                list.add(heartVoice);
            }
            return BaseResult.ok(list);
        }
        return BaseResult.ok();
    }


}
